package com.ling.pl.security.view.frame.main.register.changepassword;

import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.ling.pl.core.context.ContextHolder;
import com.ling.pl.security.feign.DefaultUserService;
import com.ling.pl.security.model.DefaultUserVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author bo.wang
 * @since 2013-2-8
 */
@Component("ling2.changePassword")
public class ChangePassword {
    @Resource
    private DefaultUserService userService;

    @Expose
    public String checkPassword(String password) throws Exception {
        DefaultUserVO user = (DefaultUserVO) ContextHolder.getLoginUser();
        if (user == null) {
            throw new RuntimeException("Please login first");
        }
        return userService.checkPassword(user.getUsername(), password);
    }

    @DataResolver
    public void changePassword(Map<String, Object> data) throws Exception {
        DefaultUserVO user = (DefaultUserVO) ContextHolder.getLoginUser();
        if (user == null) {
            throw new RuntimeException("Please login first");
        }
        userService.changePassword(user.getUsername(), (String) data.get("newPassword"));
    }

}
